package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "ChangeServlet" , urlPatterns = {"/change"})
public class ChangeServlet extends HttpServlet {
    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/login&register.jsp";
    private static final String CHANGE_FORM = "/WEB-INF/jsp/account/myaccount.jsp";
    private static final String MY_ACCOUNT_FORM = "/WEB-INF/jsp/account/myaccount.jsp";

    private String Msg;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Account loginAccount = (Account) session.getAttribute("loginAccount");
        String username = req.getParameter("username");
        String oldpassword = req.getParameter("oldpassword");
        String newpassword = req.getParameter("newpassword");
        String email = req.getParameter("email");
        String firstname = req.getParameter("firstname");
        String lastname = req.getParameter("lastname");
        String addr1 = req.getParameter("address1");
        String addr2 = req.getParameter("address2");
        String country = req.getParameter("country");
        String state = req.getParameter("state");
        String city = req.getParameter("city");
        String zip = req.getParameter("zip");
        String phone = req.getParameter("phone");
        String listOption = req.getParameter("listOption");
        String bannerOption = req.getParameter("bannerOption");
        String favouriteCategoryId = req.getParameter("favouriteCategoryId");

        loginAccount.setOldpassword(loginAccount.getPassword());

        resp.setContentType("text/plain");
//        resp.setHeader("Access-Control-Allow-Origin","http://localhost:5501");
        resp.setHeader("Access-Control-Allow-Origin","*");

        if(username != null){
            loginAccount.setUsername(username);
        }

            String password = loginAccount.getPassword();

        if(email != null){
            loginAccount.setEmail(email);
        }

        if(firstname != null){
            loginAccount.setFirstName(firstname);
        }

        if(lastname != null){
            loginAccount.setLastName(lastname);
        }

        if(addr1 != null){
            loginAccount.setAddress1(addr1);
        }

        if(addr2 != null){
            loginAccount.setAddress2(addr2);
        }

        if(country != null){
            loginAccount.setCountry(country);
        }

        if(city != null){
            loginAccount.setCity(city);
        }

        if(state != null){
            loginAccount.setState(state);
        }

        if(zip != null){
            loginAccount.setZip(zip);
        }

        if(phone != null){
            loginAccount.setPhone(phone);
        }

//        loginAccount.setListOption(listOption.equals("1"));
//        loginAccount.setBannerOption(bannerOption.equals("1"));
        if(favouriteCategoryId != null){
            loginAccount.setFavouriteCategoryId(favouriteCategoryId);
        }
        //输入密码正确，新密码不为空
//        if (oldpassword.equals(password) && newpassword != null && !(newpassword.equals(""))) {
//            loginAccount.setPassword(newpassword);
//            loginAccount.setOldpassword(password);
//            AccountService accountService = new AccountService();
//            accountService.changeAccount(loginAccount);
//            session.invalidate();//撤销会话跟踪，重新开始
//            //修改密码，则调转至登录页面
//            //resp.sendRedirect(SIGN_ON_FORM);
//            req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
//        } else if (!(oldpassword.equals(loginAccount.getPassword())) && newpassword != null && !(newpassword.equals(""))) {
//            this.Msg = "原始密码错误";
//            req.setAttribute("fixmyaccountMsg", this.Msg);
//            req.getRequestDispatcher(CHANGE_FORM).forward(req, resp);
//        } else if (oldpassword.equals(loginAccount.getPassword()) && (newpassword == null || (newpassword.equals("")))) {
//            this.Msg = "请输入新密码";
//            req.setAttribute("fixmyaccountMsg", this.Msg);
//            req.getRequestDispatcher(CHANGE_FORM).forward(req, resp);
//        }else{
//        AccountService accountService = new AccountService();
//        accountService.changeAccount(loginAccount);
//        //密码未修改，调转至myaccount
//        req.getRequestDispatcher(MY_ACCOUNT_FORM).forward(req, resp);
//        }

        System.out.println("2222");
        AccountService accountService = new AccountService();
        accountService.changeAccount(loginAccount);

        //密码未修改，调转至myaccount
        req.getRequestDispatcher(MY_ACCOUNT_FORM).forward(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Account loginAccount = (Account) session.getAttribute("loginAccount");
        String username = req.getParameter("username");
        String oldpassword = req.getParameter("oldpassword");
        String newpassword = req.getParameter("newpassword");
        String email = req.getParameter("email");
        String firstname = req.getParameter("firstname");
        String lastname = req.getParameter("lastname");
        String addr1 = req.getParameter("address1");
        String addr2 = req.getParameter("address2");
        String country = req.getParameter("country");
        String state = req.getParameter("state");
        String city = req.getParameter("city");
        String zip = req.getParameter("zip");
        String phone = req.getParameter("phone");
        String listOption = req.getParameter("listOption");
        String bannerOption = req.getParameter("bannerOption");
        String favouriteCategoryId = req.getParameter("favouriteCategoryId");

        loginAccount.setOldpassword(loginAccount.getPassword());

        resp.setContentType("text/plain");
//        resp.setHeader("Access-Control-Allow-Origin","http://localhost:5501");
        resp.setHeader("Access-Control-Allow-Origin","*");

        if(username != null){
            loginAccount.setUsername(username);
        }

        String password = loginAccount.getPassword();

        if(email != null){
            loginAccount.setEmail(email);
        }

        if(firstname != null){
            loginAccount.setFirstName(firstname);
        }

        if(lastname != null){
            loginAccount.setLastName(lastname);
        }

        if(addr1 != null){
            loginAccount.setAddress1(addr1);
        }

        if(addr2 != null){
            loginAccount.setAddress2(addr2);
        }

        if(country != null){
            loginAccount.setCountry(country);
        }

        if(city != null){
            loginAccount.setCity(city);
        }

        if(state != null){
            loginAccount.setState(state);
        }

        if(zip != null){
            loginAccount.setZip(zip);
        }

        if(phone != null){
            loginAccount.setPhone(phone);
        }

//        loginAccount.setListOption(listOption.equals("1"));
//        loginAccount.setBannerOption(bannerOption.equals("1"));
        if(favouriteCategoryId != null){
            loginAccount.setFavouriteCategoryId(favouriteCategoryId);
        }
        //输入密码正确，新密码不为空
//        if (oldpassword.equals(password) && newpassword != null && !(newpassword.equals(""))) {
//            loginAccount.setPassword(newpassword);
//            loginAccount.setOldpassword(password);
//            AccountService accountService = new AccountService();
//            accountService.changeAccount(loginAccount);
//            session.invalidate();//撤销会话跟踪，重新开始
//            //修改密码，则调转至登录页面
//            //resp.sendRedirect(SIGN_ON_FORM);
//            req.getRequestDispatcher(SIGN_ON_FORM).forward(req, resp);
//        } else if (!(oldpassword.equals(loginAccount.getPassword())) && newpassword != null && !(newpassword.equals(""))) {
//            this.Msg = "原始密码错误";
//            req.setAttribute("fixmyaccountMsg", this.Msg);
//            req.getRequestDispatcher(CHANGE_FORM).forward(req, resp);
//        } else if (oldpassword.equals(loginAccount.getPassword()) && (newpassword == null || (newpassword.equals("")))) {
//            this.Msg = "请输入新密码";
//            req.setAttribute("fixmyaccountMsg", this.Msg);
//            req.getRequestDispatcher(CHANGE_FORM).forward(req, resp);
//        }else{
//        AccountService accountService = new AccountService();
//        accountService.changeAccount(loginAccount);
//        //密码未修改，调转至myaccount
//        req.getRequestDispatcher(MY_ACCOUNT_FORM).forward(req, resp);
//        }
        System.out.println("2222");
        AccountService accountService = new AccountService();
        accountService.changeAccount(loginAccount);

        //密码未修改，调转至myaccount
        req.getRequestDispatcher(MY_ACCOUNT_FORM).forward(req, resp);
    }
}